<template>
	<view class="">
		<u-toast ref="uToast" />
		<u-navbar :is-back="false" title="邮件管理" back-icon-color='#333333' height="50" :border-bottom="false">
			<text class="iconfont icon-fanhuijian2" style="font-size:30rpx; margin-left:30rpx; font-weight:bold;"
				@click="previous_pages"></text>
			<view class="" style="margin-left: 600rpx; display: flex;align-content: center;" v-if="activeIndex==0"
				@click="$toLink('../compose_email/compose_email',{})">
				<image src="../../static/tupian88.png" mode="widthFix" style="width: 30rpx;"></image>
			</view>
		</u-navbar>
		<view class="top">
			<view class="topBox">
				<view v-for="(topItem,topIndex) of quanbu" :key="topIndex" :data-name="topIndex" @click="changeIndex"
					:class="[activeIndex==topIndex?'con':'']">
					<text></text>
					{{topItem}}
				</view>
			</view>
		</view>
		<view class="list">
			<view class="latest" v-for="(item,index) in emailList" :key="index"
				@click="$toLink('../check/check',{id:item.email_id,index:activeIndex})">
				<view class="left">
					<image :src="item.teacher_user_info?item.teacher_user_info.avatar_img:''" mode="widthFix"></image>
				</view>
				<view class="right">
					<view class="top_email">
						<view class="" :id="[changetype(item)? 'con':'']">
							{{changeWord(item)}}
						</view>
						<text :style="{marginLeft:changeMargin(item)}">{{changeTitle(item)}}</text>
						<text>{{item.startTime}}</text>
						<image src="../../static/tupian78.png" mode="widthFix"></image>
					</view>
					<view class="center">
						{{changeContent(item)}}
					</view>
					<view class="bottom">
						<text></text>
					</view>
				</view>
			</view>
		</view>
		<u-loadmore :status="status" />
	</view>
</template>

<script>
	import inbox from './children/inbox.vue'
	import request from '../../request.js'
	import time from '../../time.js'
	export default {
		data() {
			return {
				quanbu: ['收件箱', '发件箱', '草稿箱'],
				activeIndex: 0,
				back_page: 1,
				status: 'loading',
				page: 0,
				last_page: '',
				inbox_list: [],
				outbox_list: [],
				draft_list: [],
			}
		},
		onReachBottom() { //滚动到底部触发该函数
			console.log('触发了吗')
			if (this.page >= this.last_page) {
				this.status = 'nomore';
				return
			} else {
				this.status = 'loading';
			} //如果大于最后一页那么就中断函数

			this.page = ++this.page;

			setTimeout(() => {
				switch (this.activeIndex) {
					case 0:
						this.getLists(this.page);
						break;
					case 1:
						this.sendLists(this.page);
						break;
					default:
						this.tempLists(this.page);
				}
			}, 500)
		},
		computed: {
			emailList() {
				switch (this.activeIndex) {
					case 0:
						if (this.inbox_list) {
							this.inbox_list.forEach(item => {
								this.$set(item, 'startTime', time.format(item.create_time));
							})
							return this.inbox_list;
						} else {
							return [];
						}
						break;
					case 1:
						if (this.outbox_list) {
							this.outbox_list.forEach(item => {
								this.$set(item, 'startTime', time.format(item.create_time));
							})
							return this.outbox_list;
						} else {
							return [];
						}
						break;
					default:
						if (this.draft_list) {
							this.draft_list.forEach(item => {
								this.$set(item, 'startTime', time.format(item.create_time));
							})
							return this.draft_list;
						} else {
							return [];
						}
				}
			}
		},
		onLoad(option) {
			console.log(option);
			if (option.id) {
				if (option.id == 1) {
					this.back_page = 1;
				} else if (option.id == 2) {
					this.back_page = 2;
				} else {
					this.back_page = 3;
				}

				console.log(this.back_page);
			}
			if (option.email_id) {
				this.activeIndex = option.email_id;
				switch (this.activeIndex) {
					case 0:
						this.getLists(1);
						break;
					case 1:
						this.sendLists(1);
						break;
					default:
						this.tempLists(1);
				}
			} else {
				this.getLists();
			}
		},
		methods: {
			changeTag(param) {
				let str = '';
				switch (this.activeIndex) {
					case 0:
						str = param.email_info ? param.email_info.level : '';
						break;
					case 1:
						str = param.level;
						break;
					default:
						str = param.level;
				}
				return str;
			},
			changeMargin(param) {
				let bolen = '';
				let obj = param;
				let str = this.changetype(obj);
				switch (str) {
					case true:
						bolen = '15rpx';
						break;
					default:
						bolen = '65rpx';
				}
				return bolen;
			},
			changetype(param) {
				let bolen = '';
				let obj = param;
				let str = this.changeTag(obj);
				switch (str == '紧急') {
					case true:
						bolen = true;
						break;
					default:
						bolen = false;
				}
				return bolen;

			},
			changeWord(param) {
				let bolen = '';
				let obj = param;
				let str = this.changeTag(obj);
				switch (str) {
					case '紧急':
						bolen = '紧急';
						break;
					default:
						bolen = '';
				}
				return bolen;
			},
			changeTitle(param) {
				let str = '';
				switch (this.activeIndex) {
					case 0:
						str = param.email_info ? param.email_info.title : '';
						break;
					case 1:
						str = param.title;
						break;
					default:
						str = param.title;
				}
				return str;
			},
			changeContent(param) {
				let str = '';
				switch (this.activeIndex) {
					case 0:
						str = param.email_info ? param.email_info.content : '';
						break;
					case 1:
						str = param.content;
						break;
					default:
						str = param.content;
				}
				return str;
			},
			changeIndex(e) {
				console.log(e.currentTarget.dataset.name);
				let index = e.currentTarget.dataset.name;
				this.activeIndex = index;
				switch (this.activeIndex) {
					case 0:
						//切换的时候让页数重新变为零,用于标识页面状态的值也要变为原始值
						this.page = 0;
						this.getLists(1);
						break;
					case 1:
						//切换的时候让页数重新变为零,用于标识页面状态的值也要变为原始值
						this.page = 1;
						this.sendLists(1);
						break;
					default:
						//切换的时候让页数重新变为零,用于标识页面状态的值也要变为原始值
						this.page = 0;
						this.tempLists(1);
				}
			},
			//对返回上一级页面分情况跳转
			previous_pages() {
				switch (this.back_page) {
					case 1:
						this.$toLink('../career/career', {}, 3)
						break;
					case 2:
						uni.navigateBack({
							delta: 1
						})
						break;
					case 3:
						this.$toLink('../index/index', {}, 3)
						break;
				}
			},
			//收件箱
			getLists(param) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let pagination = param ? param : 1;
				let _this = this;
				request({
					url: base_url + 'teacher/email/getLists',
					data: {
						page: pagination,
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							this.inbox_list = [];
							console.log(res);
							console.log(res.data.data.data, res.data.data.last_page);
							_this.last_page = res.data.data.last_page;
							if (res.data.data.total == 0) {
								console.log('又触发吗');
								_this.status = 'nomore'
								//如果接口返回的数据为零的话，那么就中断函数
								_this.inbox_list.length = 0;
								//没有数据的话那么就将数组清空
								_this.inbox_list = [];
								return;
							};
							//页数为1的情况
							if (pagination == 1) {
								//先将原数组清空
								_this.inbox_list = [];
								console.log('进来了吗');
								if (res.data.data.data == null) {
									console.log('执行到这了吗')
									_this.inbox_list = [];
								} else {
									_this.inbox_list = res.data.data.data;
								}
								console.log(_this.inbox_list);
								_this.page = 1;
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
								//页数大于1的情况
							} else if (pagination > 1) {
								console.log(res.data.data.data);
								if (res.data.data.data == null) {
									_this.status = 'nomore'
								}
								_this.inbox_list = [..._this.inbox_list, ...res.data.data.data];
								console.log(_this.inbox_list);
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
							}
							console.log(_this.inbox_list);

						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			//发件箱
			sendLists(param) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let pagination = param ? param : 1;
				let _this = this;
				request({
					url: base_url + 'teacher/email/sendLists',
					data: {
						page: pagination,
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							this.outbox_list = [];
							console.log(res);
							console.log(res.data.data.data, res.data.data.last_page);
							_this.last_page = res.data.data.last_page;
							if (res.data.data.total == 0) {
								console.log('又触发吗');
								_this.status = 'nomore'
								//如果接口返回的数据为零的话，那么就中断函数
								_this.outbox_list.length = 0;
								//没有数据的话那么就将数组清空
								_this.outbox_list = [];
								return;
							};
							//页数为1的情况
							if (pagination == 1) {
								//先将原数组清空
								_this.outbox_list = [];
								console.log('进来了吗');
								if (res.data.data.data == null) {
									console.log('执行到这了吗')
									_this.outbox_list = [];
								} else {
									_this.outbox_list = res.data.data.data;
								}
								console.log(_this.outbox_list);
								_this.page = 1;
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
								//页数大于1的情况
							} else if (pagination > 1) {
								console.log(res.data.data.data);
								if (res.data.data.data == null) {
									_this.status = 'nomore'
								}
								_this.outbox_list = [..._this.outbox_list, ...res.data.data.data];
								console.log(_this.outbox_list);
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
							}
							console.log(_this.outbox_list);

						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			//草稿箱
			tempLists(param) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let pagination = param ? param : 1;
				let _this = this;
				request({
					url: base_url + 'teacher/email/tempLists',
					data: {
						page: pagination,
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							this.draft_list = [];
							console.log(res);
							console.log(res.data.data.data, res.data.data.last_page);
							_this.last_page = res.data.data.last_page;
							if (res.data.data.total == 0) {
								console.log('又触发吗');
								_this.status = 'nomore'
								//如果接口返回的数据为零的话，那么就中断函数
								_this.draft_list.length = 0;
								//没有数据的话那么就将数组清空
								_this.draft_list = [];
								return;
							};
							//页数为1的情况
							if (pagination == 1) {
								//先将原数组清空
								_this.draft_list = [];
								console.log('进来了吗');
								if (res.data.data.data == null) {
									console.log('执行到这了吗')
									_this.draft_list = [];
								} else {
									_this.draft_list = res.data.data.data;
								}
								console.log(_this.draft_list);
								_this.page = 1;
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
								//页数大于1的情况
							} else if (pagination > 1) {
								console.log(res.data.data.data);
								if (res.data.data.data == null) {
									_this.status = 'nomore'
								}
								_this.draft_list = [..._this.draft_list, ...res.data.data.data];
								console.log(_this.draft_list);
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
							}
							console.log(_this.draft_list);

						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.top {
		width: 100%;
		height: 65rpx;
		background: #fff;
		border-bottom: 1px solid #F2F2F2;
		overflow: hidden;


		.topBox {
			width: 100%;
			height: 65rpx;
			background: #fff;
			position: relative;
			display: flex;
			justify-content: space-evenly;

			view {
				height: 65rpx;
				line-height: 65rpx;
				width: 120rpx;
				text-align: center;
				font-size: 30rpx;
				color: #fff;
				color: #333333;

				text {
					display: none;
				}


			}

			.con {
				position: relative;
				height: 65rpx;
				line-height: 65rpx;
				color: #2F96FE;
				width: 120rpx;

				text {
					display: block !important;
					position: absolute;
					width: 120rpx;
					height: 4rpx;
					background: #2F96FE;
					bottom: 0rpx;
					left: 0rpx;
				}
			}

		}
	}

	.list {
		width: 100%;
		background: #fff;

		.latest {
			width: 710rpx;
			margin-left: 20rpx;
			border-bottom: 1px solid #F2F2F2;
			display: flex;
			height: 150rpx;
			justify-content: flex-start;

			.left {
				width: 80rpx;
				overflow: hidden;

				image {
					margin-top: 30rpx;
					margin-left: 13rpx;
					width: 62rpx;

				}
			}

			.right {
				margin-top: 20rpx;
				margin-left: 20rpx;
				width: 600rpx;

				.top_email {
					width: 100%;
					display: flex;
					justify-content: flex-start;
					height: 50rpx;

					#con {
						margin-top: 9rpx;
						width: 60rpx;
						height: 30rpx;
						border-radius: 3rpx;
						color: #fff;
						font-size: 16rpx;
						text-align: center;
						line-height: 30rpx;
						background-color: #EC654E;
					}

					view {
						&:nth-child(1) {
							margin-top: 20rpx;
							width: 12rpx;
							height: 12rpx;
							border-radius: 50%;
							background: #ABC0F7;
						}
					}

					text {
						color: #333333;
						font-size: 30rpx;

						&:nth-child(2) {
							margin-left: 15rpx;
							margin-top: 0rpx;
							width: 240rpx;
						}

						&:nth-child(3) {
							margin-top: 10rpx;
							margin-left: 15rpx;
							color: #b7b7b7;
							font-size: 20rpx;
							width: 250rpx;

						}

					}

					image {
						&:nth-child(4) {
							width: 20rpx;
							margin-top: 8rpx;
						}
					}

				}

				.center {
					width: 100%;
					margin-top: 10rpx;
					font-size: 26rpx;
					color: #333333;
					overflow: hidden;
				}

				.bottom {
					width: 100%;
					font-size: 24rpx;
					color: #999999;
				}

			}

		}
	}
</style>
